<?php
/**
 * This file acts as the "main controller" for your administrative backend. It
 * extends the standard Template Controller from Kohana and adds a layout and
 * several methods to it for rapid development.
 *
 * @package    	Kohadmin
 * @subpackage	Controller
 * @author     	Thomas Filius
 * @copyright  	(c) 2009 Nobis Stultius IT
 * @license		GNU Lesser General Public License 3
 * @version		$Id $
 */
class KohadminPage_Controller extends Template_Controller {
	/**
	 * Enter description here...
	 *
	 * @var unknown_type
	 */
	public $template = 'kohadmin/template';
	private $blocks_data = array();
	private $blocks_ignore = array();
	private $blocks_available = array("actions", "views", "shortcuts");

	/**
	 * Enter description here...
	 *
	 */
	public function __construct()
	{
		parent::__construct();

		$this->template->application_name = Kohana::config("kohadmin.application_name");

		$this->template->title = '';
		$this->template->content = "";
		$this->template->links = Kohana::config("kohadmin.controllers");
	}


	public function _render() {
		if (empty($this->template->content)) {
			$this->template->content = Kohana::lang("kohadmin.pageleftblank");
		}
		$this->template->blocks = $this->parse_blocks();

		parent::_render();
	}

	/***************************************************************************
	 * Alle functionaliteit voor het rechter blok
	 */

	public function set_title ($title)
	{
		$this->template->title = $title;
	}

	public function add_content($content)
	{
		$this->template->content .= $content;
	}

	/**
	 * Method add_shortcuts
	 */
	public function add_shortcut($shortcuts = array())
	{
		if ( ! is_array($shortcuts)) {
			return;
		}
		$this->blocks_data['shortcuts'] = $shortcuts;
	}

	/**
	 * Method: add_action - Toevoegen van een enkele actieknop in het rechterscherm
	 *
	 * @
	 */
	public function add_action($method, $controller = NULL)
	{
		if ( ! $controller) {
			$controller = uri::segment(1);
		}
		$url = $controller . "/" . $method;

		$this->blocks_data['actions'][] = array (
			'title'	=> Kohana::lang("kohadminpage.actions.".$controller."_".$method),
			"url"	=> $url
		);
	}

	public function add_overview($views = array())
	{
		if ( ! is_array($views)) {
			return;
		}
		$this->blocks_data['views'] = $views;
	}

	public function hide_block ($block = NULL)
	{
		if (is_array($block)) {
			foreach ($block as $k) {
				$this->hide_block($k);
			}
		}
		$this->blocks_ignore[] = $block;
	}

	/**
	 * private method: parse_blocks
	 *
	 * @param string $type
	 */
	private function parse_blocks ($type = NULL)
	{
		// parse single block
		if (isset($type)) {
			$view = new View ("kohadmin/kohadminpage/$type");
			$view->$type = $this->blocks_data[$type];
			return $view;
		}
		// parse all blocks
		$blocks = "";
		foreach ($this->blocks_available as $k) {
			if ( isset($this->blocks_data[$k]) AND ! in_array($k, $this->blocks_ignore)) {
				$blocks .= $this->parse_blocks($k);
			}
		}
		return ($blocks != "") ? $blocks : "&nbsp;";
	}
}
?>
